import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  BannerItem,
  newGoodItem,
  HotGoodItem,
  BrandItem,
  HomeProduct,
  Special,
} from '@/types/data'
const useHomeStore = defineStore('home', {
  state() {
    return {
      // 轮播图的数据
      bannerList: [] as BannerItem[],
      // 新鲜好物的数据
      newGoodsList: [] as newGoodItem[],
      // 人气推荐的数据
      hotGoodsList: [] as HotGoodItem[],
      // 热门品牌的数据
      brandList: [] as BrandItem[],
      // 商品主体数据
      productList: [] as HomeProduct[],
      // 最新专题数据
      specialList: [] as Special[],
    }
  },
  actions: {
    // 获取轮播图数据
    async getBannerList() {
      const res = await request.get<ApiRes<BannerItem[]>>('/home/banner')
      this.bannerList = res.data.result
    },
    // 获取新鲜好物数据
    async getNewGoodsList() {
      const res = await request.get<ApiRes<newGoodItem[]>>('/home/new')
      this.newGoodsList = res.data.result
    },
    // 获取人气推荐的数据
    async getHotGoodsList() {
      const res = await request.get<ApiRes<HotGoodItem[]>>('/home/hot')
      this.hotGoodsList = res.data.result
    },
    // 获取热门品牌的数据
    async getBrandList() {
      const res = await request.get<ApiRes<BrandItem[]>>('/home/brand')
      this.brandList = [
        ...res.data.result,
        ...res.data.result,
        ...res.data.result,
      ]
    },
    // 获取商品主体数据
    async getProductList() {
      const res = await request.get<ApiRes<HomeProduct[]>>('/home/goods')
      this.productList = res.data.result
    },
    // 获取最新专题数据
    async getSpecialList() {
      const res = await request.get<ApiRes<Special[]>>('/home/special')
      this.specialList = res.data.result
    },
  },
})
export default useHomeStore
